﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace HRdex_Final.DocumentManagement
{
    public partial class DocumentManagement : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //TextBox1.Text = (string)(Session["parentFolder"]);
            try
            {
                if (pfolder() == null)
                {
                    dsFolder.SelectCommand = "SELECT FolderID, FolderName FROM formFolder WHERE ParentFodler = " + hrdoc(SessionID());
                    Session["pFolder"] = hrdoc(SessionID());
                }
            }
            catch (Exception)
            {
                
                throw;
            }

            //else
            //{
            //    //Session["pFolder"] = foldr;
            //}
            
        }
        string sqlc;
        SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\HRdexDB.mdf;Integrated Security=True;User Instance=True");

        string pfolder()
        {
            return (string)(Session["pFolder"]);
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
           
        }
        void sqlexecute(string sqlcmd)
        {
            SqlCommand cmd = new SqlCommand(sqlcmd, conn);
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                conn.Close();
            }

        }

        protected void btnNewFolder_Click(object sender, EventArgs e)
        {
            //txtnfName.Visible = true;
            //btnCreateFolder.Visible = true;
        }

        protected void btnCreateFolder_Click(object sender, EventArgs e)
        {
            if (txtnfName.Text.Length != 0)
            {
                sqlc = "INSERT INTO formFolder (FolderName, ParentFodler) " +
                "VALUES( '" + txtnfName.Text +
                "'," + pfolder() + ")";
                dsFolder.InsertCommand = sqlc;
                dsFolder.Insert();
                //sqlexecute(sqlc);
                //lbFolder.DataBind();
                //Response.Redirect(Request.Url.AbsoluteUri);
                dsFolder.SelectCommand = "SELECT FolderID, FolderName FROM formFolder WHERE ParentFodler = " + pfolder();
                btnNew.Visible = true;
                txtnfName.Visible = false;
                btnCreateFolder.Visible = false;
            }
        }

        protected void btnrename_Click(object sender, EventArgs e)
        {
            btnrefolder.Visible = true;
            txtrename.Visible = true;
            btnrename.Visible = false;
        }

        protected void btnrefolder_Click(object sender, EventArgs e)
        {
            sqlc = "UPDATE formFolder " +
                "SET FolderName = '" + txtrename.Text +
                "' WHERE FolderID = " + lbFolder.SelectedValue;
            dsFolder.UpdateCommand = sqlc;
            dsFolder.Update();
            //sqlexecute(sqlc);
            lbFolder.SelectedItem.Text = txtrename.Text;
            btnrefolder.Visible = false;
            txtrename.Visible = false;
            btnrename.Visible = true;
        }

        protected void btnenter_Click(object sender, EventArgs e)
        {
            if (lbFolder.SelectedValue != null)
            {
                dsFolder.SelectCommand = "SELECT FolderID, FolderName FROM formFolder WHERE ParentFodler = " + lbFolder.SelectedValue;
                //foldr = lbFolder.SelectedValue;
                Session["pFolder"] = lbFolder.SelectedValue;
                //Response.Redirect(Request.Url.AbsoluteUri);
            }

        }

        protected void btndelete_Click(object sender, EventArgs e)
        {
            if (lbFolder.SelectedValue != "1" || lbFolder.SelectedValue != "2" ||lbFolder.SelectedValue != "3" ||lbFolder.SelectedValue != "5" ||lbFolder.SelectedValue != "12")
            {
                sqlc = "SELECT FileID FROM formFile WHERE FolderID = " + lbFolder.SelectedValue;
                SqlDataAdapter adpt = new SqlDataAdapter(sqlc, conn);
                DataTable tbl = new DataTable();
                adpt.Fill(tbl);
                if (tbl.Rows.Count > 0)
                {
                    sqlc = "DELETE FROM formFile WHERE FolderID = " + lbFolder.SelectedValue;
                    dsFolder.DeleteCommand = sqlc;
                    dsFolder.Delete();
                    //sqlexecute(sqlc);
                }


                sqlc = "DELETE FROM formFolder WHERE FolderID = " + lbFolder.SelectedValue;
                dsFolder.DeleteCommand = sqlc;
                dsFolder.Delete();
                dsFolder.SelectCommand = "SELECT FolderID, FolderName FROM formFolder WHERE ParentFodler = " + pfolder();
            }
            
            
            //sqlexecute(sqlc);
            
            //lbFolder.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (btncommonplace.Text == "HR Central Folder")
            {
                dsFolder.SelectCommand = "SELECT FolderID, FolderName FROM formFolder WHERE ParentFodler = 12";
                //Session["hrcp"] = hrdID();
                Session["pFolder"] = "12";
                btncommonplace.Text = "Return to Main Folder";
            }
            else
            {
                dsFolder.SelectCommand = "SELECT FolderID, FolderName FROM formFolder WHERE ParentFodler = " + hrdoc(SessionID());
                //Session["hrcp"] = "12";
                Session["pFolder"] = hrdoc(SessionID());
                btncommonplace.Text = "Shared Documents";
            }

        }
        string SessionID()
        {
            return Session["EmployeeID"].ToString();
        }
        //string hrcp()
        //{
        //    return (string)(Session["hrcp"]);
        //}
        //string hrdID()
        //{
        //    return (string)(Session["hrdoc"]);
        //}

        protected void btnSearch_Click1(object sender, EventArgs e)
        {
            if (TextBox1.Text.Length > 0)
            {
                dsFolder.SelectCommand = "SELECT FolderID, FolderName FROM formFolder WHERE FolderName like '%" + TextBox1.Text + "%' AND ParentFodler = " + pfolder();
            }
            
        }
        public string hrdoc(string userid)
        {
            try
            {
                DataTable tblFolder = new DataTable();
                string sqlslct = "SELECT catDepartment.DepartmentName " +
                        "FROM catDepartment " +
                        "INNER JOIN formEmployees ON catDepartment.DeptID = formEmployees.DeptID " +
                        "WHERE formEmployees.EmployeeID =  " + userid;
                SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\HRdexDB.mdf;Integrated Security=True;User Instance=True");
                SqlDataAdapter cmd = new SqlDataAdapter(sqlslct, conn);
                conn.Open();
                cmd.Fill(tblFolder);
                conn.Close();

                //string rtrn;
                switch (tblFolder.Rows[0][0].ToString())
                {
                    case "HR Salaries and Benefits":
                        return "2";
                    //break;
                    case "HR Recruitment and Training":
                        return "5";
                    //break;
                    case "HR Records and Info":
                        return "3";
                    default:
                        return "1";
                    //break;
                }
            }
            catch (Exception)
            {
                
                throw;
            }


        }

        protected void btnreturn_Click(object sender, ImageClickEventArgs e)
        {
            sqlc = "select ParentFodler from formFolder where FolderID = " + pfolder();
            SqlDataAdapter cmd = new SqlDataAdapter(sqlc, conn);

            DataTable dtbl = new DataTable();
            cmd.Fill(dtbl);

            string pfold = dtbl.Rows[0][0].ToString();
            string prev;
            //string hrdoc = (string)(Session["hrdoc"]);
            if (pfold == "1")
            {
                if (hrdoc(SessionID()) == "1")
                {
                    dsFolder.SelectCommand = "SELECT FolderID, FolderName FROM formFolder WHERE ParentFodler = " + pfold;
                    prev = pfold;
                }
                else
                {
                    dsFolder.SelectCommand = "SELECT FolderID, FolderName FROM formFolder WHERE ParentFodler = " + pfolder();
                    prev = pfolder();
                }
            }
            else
            {
                dsFolder.SelectCommand = "SELECT FolderID, FolderName FROM formFolder WHERE ParentFodler = " + pfold;
                prev = pfold;
            }
            
            Session["pFolder"] = prev;
        }

        protected void btnNew_Click(object sender, ImageClickEventArgs e)
        {
            txtnfName.Visible = true;
            btnCreateFolder.Visible = true;
            btnNew.Visible = false;
        }

    }
}